﻿using SimplyCleverMiddleTier;
using System.Linq;
using System.Web.Mvc;

namespace MvcSimplyCleverPart2
{
    public static class ViewHelper
    {

        public static SelectList GetStates(string stateCode)
        {

            var states = CountryProvider.GetUSStates();

            if (string.IsNullOrEmpty(stateCode))
                stateCode = states.First().Key;

            SelectList stateList = new SelectList(states, "Key", "Value", stateCode);

            return stateList;
        }


        public static SelectList GetCountries(string countryCode)
        {
            var metadataCountries = CountryProvider.GetCountries().Where(c => c.Code.Equals("CZ") || c.Code.Equals("US")).ToArray();
            SelectList countries = new SelectList(metadataCountries, "Code", "Name", countryCode);

            return countries;
        }

        public static string GetCountryName(string countryCode)
        {
            if (string.IsNullOrEmpty(countryCode))
                return string.Empty;

            var metadataCountries = CountryProvider.GetCountries();
            var metadataCountry = metadataCountries.FirstOrDefault(cm => cm.Code.Equals(countryCode));

            if (metadataCountry == null)
                return string.Empty;

            return metadataCountry.Name;
        }
    }
}